package com.gxwy.shenzhou.message;

import com.gxwy.shenzhou.HeaderRequest;
import com.gxwy.shenzhou.SplitString;
import com.gxwy.shenzhou.StringHandler;
import com.gxwy.shenzhou.Supply;

public class JinJiGuaShiRequest implements StringHandler{
   private String zhdh;
   private String mm;
   private HeaderRequest header=new HeaderRequest(true);
   
   private static final int ZHDH_LEN=32;
   private static final int MM_LEN=6;
   
   private static final int total=ZHDH_LEN+MM_LEN;
   
   private static final int ZHDH_FLAG=1;
   private static final int MM_FLAG=1;
   private int reallon=0;
   
public HeaderRequest getHeader() {
	return header;
}
public void setHeader(HeaderRequest header) {
	this.header = header;
}
public String getZhdh() {
	return zhdh;
}
public void setZhdh(String zhdh) throws Exception{
	if(zhdh==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(zhdh.getBytes("gbk").length<ZHDH_LEN){
		
		this.zhdh=Supply.supply(zhdh,ZHDH_FLAG,ZHDH_LEN);;
	}else if(zhdh.getBytes("gbk").length>ZHDH_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.zhdh = zhdh;			
	}
}
public String getMm() {
	return mm;
}
public void setMm(String mm) throws Exception{
	if(mm==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(mm.getBytes("gbk").length<MM_LEN){
	
		this.mm=Supply.supply(mm,MM_FLAG,MM_LEN);;
	}else if(mm.getBytes("gbk").length>MM_LEN){
		 Exception e=new Exception("Too long");
		 throw e;
	}else{
		this.mm = mm;			
	}
}
public String build () throws Exception{
	if(this.getHeader()==null){
		throw new Exception("Header is null");
	}
	if(this.getZhdh()==null){
		this.setZhdh("");
	}
    if(this.getMm()==null){
    	this.setMm("");
    }
    Integer totallen=header.TOTAL_LEN+total-HeaderRequest.LEN_LEN;
	this.getHeader().setLen(totallen.toString());
		return 
				this.getHeader().build()+
				this.getZhdh()+this.getMm();
	}
public void parse(String str) throws Exception{
	if(str==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(str.getBytes("gbk").length<header.TOTAL_LEN+total){
		Exception e=new Exception("Invalid request");
		throw e;
	}else{
		String headerLen=SplitString.splitString(str,reallon, reallon+header.TOTAL_LEN);
		reallon+=header.TOTAL_LEN;
		header.parse(headerLen);
		
		this.setZhdh(str.substring(reallon,ZHDH_LEN+reallon));
		reallon+=ZHDH_LEN;
		this.setMm(str.substring(reallon,reallon+MM_LEN));
	
	}
  }
public static void main(String args[]) throws Exception{
	JinJiGuaShiRequest	abc=new JinJiGuaShiRequest();
//	abc.setHeader(abc.getHeader());
    abc.setZhdh("1");
    abc.setMm("9");
   abc.parse(abc.build()) ;
   if(abc.getZhdh().trim().equals("1")){
	   System.out.println("Zhdhyes");
   }
   if(abc.getMm().trim().equals("9")){
	   System.out.println("MMyes");
   }
System.out.println(abc.getZhdh());
System.out.println(abc.getMm());
//yes

}
   
}
